package com.endbd.purchase.manage.firm.service.impl;


import com.endbd.feignapi.model.purchase.CFirm;
import com.endbd.feignapi.model.purchase.CProduct;
import com.endbd.feignapi.util.PageModel;
import com.endbd.purchase.manage.firm.dao.IFirmDao;
import com.endbd.purchase.manage.firm.model.FirmCondition;
import com.endbd.purchase.manage.firm.service.IFirmService;
import com.endbd.purchase.manage.product.dao.IProductDao;
import com.endbd.purchase.manage.product.model.ProductCondition;
import com.endbd.purchase.manage.product.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class FirmService implements IFirmService {

    @Autowired
    private IFirmDao firmDao;

    @Override
    public PageModel<CFirm> queryFirmPageByCondition(PageModel<CFirm> pageModel, FirmCondition fcon) {


        int pageNum = pageModel.getPageNum()<=0?1:pageModel.getPageNum();
        int pageSize = pageModel.getPageSize()<=0?5:pageModel.getPageSize();

        PageHelper.startPage(pageNum,pageSize);
        List<CFirm> firms = firmDao.queryFirmCondition(fcon);

        PageInfo<CFirm> info = new PageInfo<CFirm>(firms);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int)info.getTotal());
        pageModel.setData(firms);
        return pageModel;
    }

    @Override
    public List<String> firmName(FirmCondition fcon) {
        return firmDao.firmName(fcon);
    }

    @Override
    public void addFirm(CFirm firm) {
        firmDao.addFirm(firm);
    }

    @Override
    public void alterFirm(CFirm firm) {
        firmDao.alterFirm(firm);
    }

    @Override
    public void lorFirm(FirmCondition fcon) {
        if (fcon.getStatus()==1){
            firmDao.logFirm(fcon.getFirmId());
        }else {
            firmDao.recoveryFirm(fcon.getFirmId());
        }
    }
}
